﻿//----------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Keith B. Rimington">
//
//   Copyright (c) 2011 Keith B. Rimington
//
//   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
//     documentation files (the "Software"), to deal in the Software without restriction, including without limitation
//     the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
//     to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//   The above copyright notice and this permission notice shall be included in all copies or substantial portions of
//     the Software.
//
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//     THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
//     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//     DEALINGS IN THE SOFTWARE.
//
// </copyright>
//----------------------------------------------------------------------------------------------------------------------

namespace Noc.Pomodoro
{
    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows;
    using System.Windows.Shell;
    using Noc.Pomodoro.Core;
    using Noc.Pomodoro.ViewModels;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            var viewModel = new MainWindowViewModel() { Date = DateTime.Today };
            viewModel.PropertyChanged += this.OnModelPropertyChanged;
            DataContext = viewModel;
            this.BindData();
        }

        /// <summary>
        /// Binds data to the form; usually called after the calendar selection or configuration changes.
        /// </summary>
        private void BindData()
        {
            var repository = new PomodoroRepository();
            var viewModel = this.DataContext as MainWindowViewModel;
            var tasks = repository.GetTasksByDate(viewModel.Date);
            viewModel.Items.Clear();
            tasks.ToList().ForEach(task => viewModel.Items.Add(task));
        }

        /// <summary>
        /// Called when properties on the view model change; primarily to issue a BindData() when configuration changes.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void OnModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Date")
            {
                this.BindData();
            }
        }

        /// <summary>
        /// Called when task operations are raised, to commit the changes to the database.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Noc.Pomodoro.Core.TaskEventArgs"/> instance containing the event data.</param>
        private void OnTaskOperation(object sender, TaskEventArgs e)
        {
            var repository = new PomodoroRepository();
            var task = e.Task;

            if (e.Operation == TaskOp.Insert)
            {
                repository.AddTask(task);
            }
            else
            {
                repository.UpdateTask(task);
            }

            if (e.Operation == TaskOp.StartPomodoro)
            {
                this.timer.Time = new TimeSpan(0, 0, 10);//new TimeSpan(0, 25, 0);
                this.timer.Start();
                this.taskbarInfo.ProgressState = TaskbarItemProgressState.Paused;
            }
            else if (e.Operation == TaskOp.VoidPomodoro)
            {
                this.timer.Stop();
                this.taskbarInfo.ProgressState = TaskbarItemProgressState.Error;
            }
        }

        private void OnTimerFinished(object sender, RoutedEventArgs e)
        {
            var repository = new PomodoroRepository();
            var viewModel = this.DataContext as MainWindowViewModel;
            var tasks = viewModel.Items.ToList();
            if (viewModel.Date != DateTime.Today)
            {
                tasks = repository.GetTasksByDate(DateTime.Today).ToList();
            }

            var task = tasks.FirstOrDefault(t => t.IsRunning);
            if (task != null)
            {
                var pom = task.Pomodoros.FirstOrDefault(p => p.State == ViewModels.Lookup.PomodoroState.Running);
                if (pom != null)
                {
                    pom.State = ViewModels.Lookup.PomodoroState.Completed;
                    repository.UpdateTask(task);
                    this.taskbarInfo.ProgressState = TaskbarItemProgressState.Normal;
                }
            }
        }
    }
}